from PyQt5 import QtWidgets, QtGui, QtCore
import sys
from replace_ui import Ui_Form
from PyQt5.QtWidgets import QFileDialog
import re
import os
import traceback





class mywindow(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(mywindow,self).__init__()
        self.setupUi(self)

    def selectfolder(self):
        filename=QFileDialog.getExistingDirectory(self,directory=r'E:\tomcat\2.dezhou')
        print(filename)
        self.text_folder.setText(filename)
    def beginChange(self):
        try:
            if self.text_folder.toPlainText()=='':
                self.text_info.setText('未选择路径')
                return
            if self.text_ip.text()=='':
                self.text_info.setText('未填写Ip')
                return
            if self.text_estate.text() =='' and self.mod_estate.checkState()==2:
                self.text_info.setText('未填写业务库名')
                return
            if self.text_pfuser.text() == '' and self.mod_pfuser.checkState()==2:
                self.text_info.setText('未填写平台库名')
                return
            requestIem={}
            requestIem['folder']=self.text_folder.toPlainText()
            requestIem['ip']=self.text_ip.text()
            requestIem['modpfuser']=self.mod_pfuser.checkState()
            requestIem['modestate']=self.mod_estate.checkState()
            requestIem['pfusername']=self.text_pfuser.text()
            requestIem['estatename']=self.text_estate.text()
            self.myreplcae(requestIem,self.textBrowser)
        except Exception as r:
            print(traceback.print_exc())

    @staticmethod
    def myreplcae(requestIem,textBrowser):
        fileList = [r'webapps\cas\WEB-INF\deployerConfigContext.xml',  # cas
                    r'webapps\FQuery\WEB-INF\classes\hibernate.cfg.xml',  # foquery
                    r'webapps\MsgService\WEB-INF\classes\applicationContext.xml',
                    r'webapps\MsgService\WEB-INF\classes\spring-rabbitmq.xml',
                    # MsgService
                    r'webapps\PFFrame\flex\config\appconfig.xml',
                    r'webapps\PFFrame\WEB-INF\classes\config\pfframe.properties', r'webapps\PFFrame\WEB-INF\web.xml',
                    # PFFrame
                    r'webapps\UniEstate\WEB-INF\classes\config\uniestate.properties',
                    r'webapps\UniEstate\WEB-INF\classes\config\case-struts.xml',  # UniEstate
                    r'webapps\WFService\WEB-INF\classes\DBcfg.xml']  # wfservice
        re_ip = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
        re_estate = re.compile(r'(estate(_[a-z]{2})?)')
        re_pfuser = re.compile(r'(pfuser(_[a-z]{2})?)')

        replace_ip = requestIem['ip']
        replace_pfuser = requestIem['pfusername']
        relplace_estate = requestIem['estatename']
        root_folder = requestIem['folder']
        modpfuser= requestIem['modpfuser']
        modestate=requestIem['modestate']
        textBrowser.append('需要修改的Tomcat所在路径{}'.format( root_folder))
        textBrowser.append('需要修改的IP{}'.format(replace_ip))
        textBrowser.append('{}修改平台库名称'.format('需要' if modpfuser==2 else '不需要'))
        textBrowser.append('{}修改业务库名称'.format('需要' if modestate == 2 else '不需要'))

        textBrowser.append('开始进行')
        print(fileList)
        for name in fileList:
            fullpath = os.path.join(os.path.abspath(root_folder), name)
            textBrowser.append('开始修改文件{}'.format(fullpath))
            try:
                content = ''
                with open(fullpath, 'r',encoding='utf-8') as f:
                    content = f.read()
                    content = re_ip.sub(replace_ip, content)
                    if modestate==2:
                        content = re_estate.sub(relplace_estate, content)
                    if modpfuser==2:
                        content = re_pfuser.sub(replace_pfuser, content)


                with open(fullpath, 'w',encoding='utf-8') as wf:
                    print('需要加入的内容', content)
                    wf.write(content)


            except Exception as e:
                print(traceback.print_exc())
                textBrowser.append('<span style=\" color:red;\">{}</span>'.format('打开文件{}出错:{}'.format(fullpath,str(e))))
                continue
            textBrowser.append('完成修改文件{}'.format(fullpath))


if __name__ == '__main__':
    app=QtWidgets.QApplication(sys.argv)
    window=mywindow()
    window.show()
    sys.exit(app.exec_())